package site.isscloud.web.controller.project;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.isscloud.common.annotation.Log;
import site.isscloud.common.core.controller.BaseController;
import site.isscloud.common.core.domain.AjaxResult;
import site.isscloud.common.enums.BusinessType;
import site.isscloud.project.domain.*;
import site.isscloud.project.service.*;
import site.isscloud.common.utils.poi.ExcelUtil;
import site.isscloud.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 *
 * @author Junhuang
 * @date 2025-10-07
 */
@RestController
@RequestMapping("/project/project")
public class PjProjectController extends BaseController
{
    @Autowired
    private IPjProjectService pjProjectService;

    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(PjProject pjProject)
    {
        startPage();
        List<PjProject> list = pjProjectService.selectPjProjectList(pjProject);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PjProject pjProject)
    {
        List<PjProject> list = pjProjectService.selectPjProjectList(pjProject);
        ExcelUtil<PjProject> util = new ExcelUtil<PjProject>(PjProject.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{pjId}")
    public AjaxResult getInfo(@PathVariable("pjId") Long pjId)
    {
        return success(pjProjectService.selectPjProjectByPjId(pjId));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PjProject pjProject)
    {
        return toAjax(pjProjectService.insertPjProject(pjProject));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PjProject pjProject)
    {
        return toAjax(pjProjectService.updatePjProject(pjProject));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pjIds}")
    public AjaxResult remove(@PathVariable Long[] pjIds)
    {
        return toAjax(pjProjectService.deletePjProjectByPjIds(pjIds));
    }

    /**
     * 获取统计信息1 - 面板数据
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/statistics1")
    public AjaxResult getStatistics1(@RequestHeader("token") String token) {
        logger.info("Getting statistics1 data");
        return AjaxResult.success(new Object() {
            @SuppressWarnings("unused")
            public final Object data = new Object() {
                public final Object[] panels = new Object[] {
                        new Object() {
                            public final String title = "支付订单";
                            public final int value = 51;
                            public final String unit = "年";
                            public final String unitColor = "success";
                            public final String subTitle = "总支付订单";
                            public final int subValue = 51;
                            public final String subUnit = "";
                        },
                        new Object() {
                            public final String title = "订单量";
                            public final int value = 555;
                            public final String unit = "周";
                            public final String unitColor = "danger";
                            public final String subTitle = "转化率";
                            public final String subValue = "60%";
                            public final String subUnit = "";
                        },
                        new Object() {
                            public final String title = "销售额";
                            public final double value = 3.74;
                            public final String unit = "年";
                            public final String unitColor = "";
                            public final String subTitle = "总销售额";
                            public final double subValue = 3.74;
                            public final String subUnit = "";
                        },
                        new Object() {
                            public final String title = "新增用户";
                            public final int value = 22;
                            public final String unit = "年";
                            public final String unitColor = "warning";
                            public final String subTitle = "总用户";
                            public final int subValue = 22;
                            public final String subUnit = "人";
                        }
                };
            };
        });
    }

    /**
     * 获取统计信息2 - 商品和订单状态
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/statistics2")
    public AjaxResult getStatistics2(@RequestHeader("token") String token) {
        logger.info("Getting statistics2 data");
        return AjaxResult.success(new Object() {
            @SuppressWarnings("unused")
            public final Object data = new Object() {
                public final Object[] goods = new Object[] {
                        new Object() {
                            public final String label = "审核中";
                            public final int value = 2;
                        },
                        new Object() {
                            public final String label = "销售中";
                            public final int value = 122;
                        },
                        new Object() {
                            public final String label = "已下架";
                            public final int value = 62;
                        },
                        new Object() {
                            public final String label = "库存预警";
                            public final int value = 6;
                        }
                };
                public final Object[] order = new Object[] {
                        new Object() {
                            public final String label = "待付款";
                            public final int value = 171;
                        },
                        new Object() {
                            public final String label = "待发货";
                            public final int value = 16;
                        },
                        new Object() {
                            public final String label = "已发货";
                            public final int value = 1;
                        },
                        new Object() {
                            public final String label = "退款中";
                            public final int value = 17;
                        }
                };
            };
        });
    }

    /**
     * 获取统计信息3 - 时间序列数据
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/statistics3")
    public AjaxResult getStatistics3(@RequestHeader("token") String token,
                                     @RequestParam String type) {
        logger.info("Getting statistics3 data, type={}", type);

        final Object data_week = new Object() {
            public final String[] x = new String[] {
                    "07-24", "07-23", "07-22", "07-21", "07-20", "07-19", "07-18"
            };
            public final int[] y = new int[] {
                    51, 0, 1, 1, 0, 2, 1
            };
        };

        final Object data_hour = new Object() {
            public final String[] x = new String[] {
                    "00","23","22","21","20","19","18","17","16","15","14","13","12","11","10","09","08","07","06","05","04","03","02","01"
            };
            public final int[] y = new int[] {
                    0,0,0, 0, 0,4,6,3,0,6,3,0,0,11,18,0,0,0,0,0,0,0,0,0
            };
        };

        final Object data_month = new Object() {
            public final String[] x = new String[] {
                    "07-24", "07-23", "07-22", "07-21", "07-20", "07-19", "07-18",
                    "07-17", "07-16", "07-15", "07-14", "07-13", "07-12", "07-11",
                    "07-10", "07-09", "07-08", "07-07", "07-06", "07-05", "07-04",
                    "07-03", "07-02", "07-01", "06-30", "06-29", "06-28", "06-27",
                    "06-26", "06-25"
            };
            public final int[] y = new int[] {
                    51, 0, 1, 1, 0, 2, 1, 0, 0, 0, 2, 0, 1, 0, 5, 1, 0, 0, 0, 1,
                    0, 0, 4, 6, 0, 0, 0, 0, 0, 0
            };
        };

        Object result = data_week;
        if(type.equals("month")) {
            result = data_month;
        }
        if(type.equals("hour")) {
            result = data_hour;
        }

        return AjaxResult.success(result);
    }
}